package net.synck.gcontacts.ui;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import net.synck.gcontact.test.ContactEditorComposite2;
import net.synck.gcontacts.editor.ContactEditorInput;

import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.util.ServiceException;

public class ContactEditor extends EditorPart {

	public static final String ID = "net.synck.gcontacts.ui.ContactEditor"; // TODO Needs to be whatever is mentioned in plugin.xml
	private ContactEditorComposite2 top = null;

	private EditorContentModificationListener contentChangeListener 
		= new EditorContentModificationListener();  //  @jve:decl-index=0:
	
	private boolean isDirty = false;
	private boolean isLoadingContent = false;
	
	private ContactEntry contact = null; 
	
	
	class  EditorContentModificationListener implements ModifyListener, SelectionListener {
		public void modifyText(ModifyEvent e) {
			setDirty(true);	
		}
		public void widgetDefaultSelected(SelectionEvent e) {
			// TODO Auto-generated method stub			
		}
		public void widgetSelected(SelectionEvent e) {
			setDirty(true);			
		}		
		
	}
	
	@Override
	public void doSave(IProgressMonitor monitor) {
						
		collectContent();
		
		boolean error = false;
		
		try {
			monitor.beginTask("updating contact data", monitor.UNKNOWN);
			ContactEntry updatedContact = net.synck.gcontacts.Activator.getDefault().getContactsService()
				.update(new URL(contact.getEditLink().getHref()), contact);
			if (updatedContact != null) {
				contact = updatedContact;
				setPartName(getContactTitleName());
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			error = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			error = true;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			error = true;
		} finally {
			monitor.done();
		}
		
		if (!error)
			setDirty(false);
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		if (!(input instanceof ContactEditorInput))
			throw new PartInitException("Unsupported editor input");
		
		setSite(site);
		setInput(input);
		
		try {
			retrieveContact();
		} catch (Exception e) {
			throw new PartInitException("can not retived contact data", e);
		}
		
		setPartName(getContactTitleName());				
	}
		
	private String getContactTitleName() {
		if (contact == null)
			return null;
		String name =
			contact.getTitle().getPlainText();
		if ((name == null || name.isEmpty()) && contact.getEmailAddresses().size() > 0)
			name = contact.getEmailAddresses().get(0).getAddress();			
		return name;
	}
	
	@Override
	public boolean isDirty() {
		return isDirty;
	}

	public void setDirty(boolean dirty) {
		if (!isLoadingContent) {
			isDirty = dirty;
			firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
		}
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		//top = new ContactEditorComposite(parent, SWT.NONE);
		top = new ContactEditorComposite2(parent, SWT.NONE);
		populateContent();

		//top.setModifyListener(contentChangeListener);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	
	private void retrieveContact() throws Exception {
		URL contactUrl = null;
		
		contactUrl = new URL( ((ContactEditorInput)getEditorInput()).getContactId());		
		
		contact = net.synck.gcontacts.Activator.getDefault().getContactsService()			
			.getEntry(contactUrl, ContactEntry.class );
		
	}
	
	private void populateContent() {				
		if (contact == null)
			return;			
		//top.setContactName(contact.getTitle().getPlainText());
		top.setInput(contact);
	}
	
	private void collectContent() {
		//contact.setTitle(new PlainTextConstruct(top.getContactName()));
	}
	
}
